package aufgabe_01;

import java.io.*;
import java.io.ObjectInputStream.GetField;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;

public class FileWriterFactory {
	private static HashMap<String, FileWriter> fwmap;
	private static Vector<String> keyvec ;
	private static FileWriterFactory instance = null;
	private static int objcount = 0;

	private FileWriterFactory() {

		fwmap = new HashMap<String,FileWriter>();
		keyvec = new Vector<String>();
	}

	public static FileWriterFactory getInstance() {
		
		if (instance != null) {
			return instance;
		} else {
			return new FileWriterFactory();
		}
	}
	
	public FileWriter getFileWriter( String s )throws IOException{
		if( fwmap.containsKey(s)){
			System.out.println(objcount);
			return fwmap.get(s);
		}
		else{
		FileWriter tempfw = new FileWriter(s);
		fwmap.put(s, tempfw);
		keyvec.add(s);
		objcount++;
		return tempfw;
		}
	}
	
	public void close()throws Throwable{
		for (Iterator<String> iterator = keyvec.iterator(); iterator.hasNext();) {
			String type = (String) iterator.next();
			fwmap.get(type).close();
		}
	}
}
